package com.woniuxy.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.business.config.KillProducer;
import com.woniuxy.business.dao.CouponDao;
import com.woniuxy.business.service.CouponService;
import com.woniuxy.commons.entity.Coupon;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 雷珉
 * @date 2022年06月08日 16:30
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Resource
    private CouponDao couponDao;
    @Resource
    private RedisTemplate<String,Integer> redisTemplate;
    @Resource
    private KillProducer killProducer;

    @Override
    public ResponseResult<List<Coupon>> findAll() {
        List<Coupon> all = couponDao.findAll();
        return new ResponseResult<>(200,"查询成功",all,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<PageInfo<Coupon>> findAllByCondition(QueryEntity<Coupon> queryEntity) {
        //开启分页
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        //排序
        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }
        //封装数据
        List<Coupon> all = couponDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Coupon> pageInfo = PageInfo.of(all);
        return new ResponseResult<>(200,"查询成功",pageInfo,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<Coupon> findOne(int id) {
        Coupon one = couponDao.findOne(id);
        return new ResponseResult<>(200,"查询成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult addOne(Coupon Coupon) {
        couponDao.addOne(Coupon);
        return new ResponseResult<>(200,"添加成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteOne(int id) {
        couponDao.deleteOne(id);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteBatch(int[] ids) {
        couponDao.deleteBatch(ids);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult updateOne(Coupon Coupon) {
        couponDao.updateOne(Coupon);
        return new ResponseResult<>(200,"更新成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult active(Coupon coupon) {
        String key="hospital:coupon:"+coupon.getId();
        //根据active判断是上架还是下架
        if (coupon.getActive()==1){
            //下架，先同步数据库、redis的库存
            Integer stock = redisTemplate.opsForValue().get(key);
            coupon.setTotal(stock);
            couponDao.updateOne(coupon);
            //再删除redis中的数据
            redisTemplate.delete(key);
            return new ResponseResult<>(200,"下架成功",null,ResStatus.SUCCESS);
        }else {
            //上架，先去数据库查最新库存
            Coupon newOne = couponDao.findOne(coupon.getId());
            //修改数据库
            newOne.setActive(2);
            couponDao.updateOne(newOne);
            //再加入redis，key为hospital:coupon:id，value为数量
            redisTemplate.opsForValue().set(key, newOne.getTotal());
            return new ResponseResult<>(200,"上架成功",null,ResStatus.SUCCESS);
        }
    }

    @Override
    public ResponseResult getCoupon(int id, int userId) {
        //1、检查库存
        int stock = redisTemplate.opsForValue().get("hospital:coupon:"+id);
        if(stock>0){
            //2、如果有库存，加入抢购队列
            killProducer.send(id+":"+userId);
            return new ResponseResult(200,"正在拼命抢，请等待结果~",null, ResStatus.SUCCESS);
        }
        //3、如果没库存
        return new ResponseResult(200,"券已经抢完了！",null, ResStatus.SUCCESS);
    }
}

